package com.javasm.controller.goods;


import com.javasm.common.http.AxiosResult;
import com.javasm.common.page.PageResult;
import com.javasm.common.perm.HasPerm;
import com.javasm.common.valid.addGroup;
import com.javasm.controller.base.BaseController;
import com.javasm.domin.criteria.goods.GoodsCriteria;
import com.javasm.domin.criteria.goods.ProductCriteria;
import com.javasm.domin.entity.Admin;
import com.javasm.domin.entity.goods.Goods;
import com.javasm.domin.vo.goods.GoodsVO;
import com.javasm.domin.vo.goods.ProductVo;
import com.javasm.service.goods.IGoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whl
 * @since 2022-01-16
 */
@RestController
@RequestMapping("goods")
@RequiredArgsConstructor
public class GoodsController extends BaseController {

    private final IGoodsService iGoodsService;

    /**
     * 分页条件查询
     */
    @GetMapping
    public AxiosResult<PageResult<GoodsVO>> list(GoodsCriteria goodsCriteria) {
        PageResult<GoodsVO> list = iGoodsService.searchPage(goodsCriteria);
        return AxiosResult.success(list);
    }



    /**
     * 添加
     */
    @PostMapping
    public AxiosResult<Void> add(@Validated(addGroup.class) @RequestBody Goods goods) {

        return toAxios(iGoodsService.save(goods));
    }



    /**
     * 根据id查询
     */
    @GetMapping("{id}")
    public AxiosResult<GoodsVO> findById(@PathVariable Long id) {
        GoodsVO byId = iGoodsService.getvoById(id);

        return AxiosResult.success(byId);
    }


    /**
     * 修改
     */
    @PutMapping
    public AxiosResult<Void> update(@RequestBody Goods goods) {
        return toAxios(iGoodsService.update(goods));
    }

    /**
     * 删除
     */
    @DeleteMapping("{id}")
    public AxiosResult<Void> delete(@PathVariable Long id) {
        return toAxios(iGoodsService.deleteById(id));
    }


    /**
     * 批量删除
     */
    @DeleteMapping("batch/{ids}")
    public AxiosResult<Void> batch(@PathVariable List<Long> ids) {
        return toAxios(iGoodsService.batchDeleteByIds(ids));
    }
}
